package com.luyuan.crm.question.web.converter;

import java.io.Serializable;

import javax.annotation.Resource;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.springframework.stereotype.Component;

import com.luyuan.crm.question.model.Question;
import com.luyuan.crm.question.service.QuestionService;
import com.ruijie.util.StringUtils;
import com.ruijie.web.jsf.converter.AbstractConverter;

@Component("questionConverter")
public class QuestionConverter extends AbstractConverter implements Converter, Serializable {

    private static final long serialVersionUID = 1L;

    @Resource(name = "questionService")
    private QuestionService questionService;

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
        if (StringUtils.hasText(value)) {
            Question question = questionService.get(value);
            return question;
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
        if (object == null) {
            return null;
        }
        Question question = (Question) object;
        return question.getId();
    }

}
